package com.cyq.day10;

import javax.swing.*;

public class StaffManagerSystem {
    static String passWord = "111111";
    static String userName = "aaaaaa";
    //工号，姓名，工资
    static String[][] staff = {
            {"工号", "姓名", "工资"},
            {"000", "张三", "5000"},
            {"001", "李四", "6000"},
            {"002", "王五", "7000"},
    };

    public static void main(String[] args) {

        login();


    }

    public static void login() {
        for (int i = 0; i < 3; i++) {
            String input = JOptionPane.showInputDialog(null, "请输入用户名");
            String pwdInput = JOptionPane.showInputDialog(null, "请输入密码");
            if (input.equals(userName) && pwdInput.equals(passWord)) {
                menu();
            } else {
                JOptionPane.showMessageDialog(null, "用户名或密码错误");
            }
        }
        JOptionPane.showMessageDialog(null, "非法用户");
        System.exit(0);

    }

    public static void menu() {
        while (true) {
            String input = JOptionPane.showInputDialog(null, "请选择：\n+" +
                    "1.添加员工\n+" +
                    "2.显示员工信息\n+" +
                    "3.删除员工\n+" +
                    "4.查询员工信息\n+" +
                    "5.修改员工工资\n+" +
                    "6.按工资排序显示工资单\n+" +
                    "7.退出");
            switch (Integer.parseInt(input)) {
                case 1:
                    addStaff();
                    break;
                case 2:
                    showStaff();
                    break;
                case 3:
                    deleteStaff();
                    break;
                case 4:
                    searchStaff();
                    break;
                case 5:
                    changeWage();
                    break;
                case 6:
                    showBywage();
                    break;
                case 7:
                    System.out.println("系统退出");
                    System.exit(0);
                default:
                    JOptionPane.showMessageDialog(null, "输入有误请重新输入");
                    break;
            }
        }
    }

    public static void addStaff() {
        /*
        添加员工方法
         */
        String inputNum = JOptionPane.showInputDialog(null, "请输入工号");
        for (int i = 0; i < staff.length; i++) {
            //用equals时把对象放前面，可以避免空指针异常
            if (staff[i][0].equals(inputNum)) {
                JOptionPane.showMessageDialog(null, "该工号已存在，请重新输入");
            }
        }
        String inputName = JOptionPane.showInputDialog(null, "请输入姓名");
        String inputWage = JOptionPane.showInputDialog(null, "请输入工资");
        String[][] newStaff = new String[staff.length + 1][staff[0].length];
        for (int i = 0; i < staff.length; i++) {
            for (int j = 0; j < staff[i].length; j++) {
                newStaff[i][j] = staff[i][j];
            }
        }
        newStaff[staff.length][0] = inputNum;
        newStaff[staff.length][1] = inputName;
        newStaff[staff.length][2] = inputWage;
        staff = newStaff;
    }

    public static void showStaff() {
        /*
        显示员工信息方法
         */
        JOptionPane.showMessageDialog(null, aryChange(staff));
    }

    public static void deleteStaff() {
        /*
        删除员工方法
         */
        String[][] newStaff = new String[staff.length - 1][staff[0].length];
        String inputName = JOptionPane.showInputDialog(null, "请输入姓名");
        int temp = 0;
        for (int i = 0; i < staff.length; i++) {
            if (!inputName.equals(staff[i][1])) {
                temp++;
            }
        }
        if (temp == staff.length) {
            JOptionPane.showMessageDialog(null, "查无此人");
            return;
        }
        for (int i = 0; i < newStaff.length; i++) {
            for (int j = 0; j < newStaff[i].length; j++) {
                if (!inputName.equals(staff[i][1])) {
                    newStaff[i][j] = staff[i][j];
                } else {
                    newStaff[i][j] = staff[i + 1][j];
                }
            }
        }
        staff = newStaff;

    }

    public static void searchStaff() {
        /*
        查询员工信息方法
         */
        String name = JOptionPane.showInputDialog(null, "请输入姓名");
        for (int i = 0; i < staff.length; i++) {
            if (name.equals(staff[i][1])) {
                JOptionPane.showMessageDialog(null, aryChange(staff)[i]);
                return;
            }
        }
        JOptionPane.showMessageDialog(null, "查无此人");
    }

    public static void changeWage() {
        /*
        修改员工工资方法
         */
        String inputName = JOptionPane.showInputDialog(null, "请输入姓名");
        int temp = 0;
        for (int i = 0; i < staff.length; i++) {
            if (!inputName.equals(staff[i][1])) {
                temp++;
            }
        }
        if (temp == staff.length) {
            JOptionPane.showMessageDialog(null, "查无此人");
            return;
        }
        String inputWage = JOptionPane.showInputDialog(null, "请重新输入工资");
        for (int i = 0; i < staff.length; i++) {
            if (inputName.equals(staff[i][1])) {
                staff[i][2] = inputWage;
            }
        }
    }

    public static void showBywage() {
        /*
        按照工资排序显示工资单方法
         */
        for (int j = 0; j < staff.length - 1; j++) {
            for (int i = 1; i < staff.length - 1 - j; i++) {
                if (Integer.parseInt(staff[i][2]) < Integer.parseInt(staff[i + 1][2])) {
                    String temp = "";
                    temp = staff[i + 1][2];
                    staff[i + 1][2] = staff[i][2];
                    staff[i][2] = temp;
                }
            }
        }
        showStaff();
    }

    /**
     * 封装方法，数组转换显示
     * @param ary
     * @return
     */
    public static String[] aryChange(String[][] ary) {
        String[] newAry = new String[ary.length];
        for (int i = 0; i < ary.length; i++) {
            newAry[i] = "";
            for (int j = 0; j < ary[i].length; j++) {
                newAry[i] += "   " + ary[i][j];
            }

        }
        return newAry;
    }
    /*
    封装方法，返回整形，找不到为-1，找到了则返回下标值
     */
    public static int find(String[][] ary,String value,int index){
        for (int i=0;i<ary.length;i++){
            if (ary[i][index].equals(value)){
                return i;
            }
        }
        return -1;
    }
}
